<?php
	
	
	if(getRequestParameter("get_payment_form") !== false)
	{
		$paymentMethod = getRequestParameter("get_payment_form");
		
		$messages = array();
		$errors = array();
		$params = array();
		
		$client = getLoginData();
			
		$checkPassword = false;
		
		if(!isset($client["id"]))
		{
			if(getRequestParameter("create_account") !== false && getRequestParameter("create_account") == 1)
			{
				$params["create_account"] = 1;
				$checkPassword = true;
			}
		}
		validateClient($params, $errors, $client, $checkPassword);
		validateDeliveryDetails($params, $errors);
		
		if(isset($client["id"]))
		{
			$params["email"] = $client["email"];
		}
		
		if(count($errors) == 0)
		{
			if($paymentMethod == 3)
			{
				getCheckoutForm($params);
			}
			elseif($paymentMethod == 2)
			{
				getPayPalForm($params);
			}
		}
	}
	
	if(getRequestParameter("check-order") !== false)
	{
		
		$products = getCartProducts();
		if(count($products) > 0)
		{
			$minimalOrder = get_setting("minim_order");
			if($products[0]["products_price"] < $minimalOrder)
			{
				$message["error"] = translate("cart_minimal_order")." : " . makePriceInterval($minimalOrder,0,get_setting("currency"));
				echo json_encode($message);
			}
		}
		
		return false;
	}
	
	if(getRequestParameter("checkout") !== false)
	{
		$messages = array();
		$errors = array();
		$params = array();
		$step = 1;
		$errorCount = 0;
		
		$params["create_account"] = 0;
		
		if(getRequestParameter("step") !== false)
		{
			if( getRequestParameter("step") > 0)
			{
				$step = getRequestParameter("step");
			}
			else
			{
				$messages["step"] = translate("checkout_invalid_step");
				$errorCount++;
			}
		}
		if($step > 1)
		{
			$client = getLoginData();
			
			$checkPassword = false;
			
			if(!isset($client["id"]))
			{
				if(getRequestParameter("create_account") !== false && getRequestParameter("create_account") == 1)
				{
					$params["create_account"] = 1;
					$checkPassword = true;
				}
			}
			validateClient($params, $errors, $client, $checkPassword);
			if(count($errors) > 0)
			{
				$step = 0;
			}
			else
			{
				if($step > 2)
				{
					validateDeliveryDetails($params, $errors);
					
					if(count($errors) > 0)
					{
						$step = 0;
					}
					elseif($step > 3)
					{
						if(getRequestParameter("payment_method") != false && getRequestParameter("payment_method") != "")
						{
							$paymentMethod = getRequestParameter("payment_method");
							if($paymentMethod == 1)
							{
								$params["payment_method_text"] = translate("payment_method_cash_on_delivery");
	  							$params["payment_method"] = 1;
	    
							}
							elseif($paymentMethod == 2)
							{
								  $params["payment_method_text"] =  translate("payment_method_paypal");
								  $params["payment_method"] = 2;
							}
							elseif($paymentMethod == 3)
							{
								$params["payment_method_text"] =  translate("payment_method_card");
								$params["payment_method"] = 3;
							}
							else
							{
								$errors["payment_method"] = translate("checkout_invalid_payment_method");
							}
						}
						else
						{
							$errors["payment_method"] = translate("checkout_error_select_payment_method");
						}
						
						if(count($errors) > 0)
						{
							$step = 0;
						}
						elseif($step > 4)
						{
							$orderID = finalizeOrder($params, $errors);
							$step = 4;
							
							if(count($errors) == 0)
							{
								$products = getCartProducts();
								foreach($products as $product)
								{
									$query = "UPDATE stocks SET quantity = quantity - ?, buy_count =  buy_count + ? 
												WHERE id = ?";
									global $connection;
									$connection->execute_query($query, array($product["quantity"], $product["quantity"], $product["stock_id"]));	
								}
								if(isset($_SESSION["cart"]))
								{
									unset($_SESSION["cart"]);
								}
								
								if(isset($_SESSION["coupons"]))
								{
									unset($_SESSION["coupons"]);
								}
								$messages["success"] = translate("order_success");
								$messages["order_id"] = $orderID;
							}
						}
					}
					
				}
			}
		}
			
		if(count($errors) > 0)
		{
			$messages = $errors;
			$messages["error_count"] = count($errors);
		}
		else
		{
			$messages["error_count"] = 0;
		}
		
		$messages["step"] = $step;
		$messages["params"] = $params;
		
		echo json_encode($messages);
		
		return false;
	}
?>